<?php

namespace app\h7_tool\controller\web;

use app\wc_card\facade\service\MemberCardServiceFacade;
use app\wc_card\facade\service\MemberServiceFacade;
use app\wc_card\facade\service\OrderRechargeServiceFacade;
use xsframe\base\AdminBaseController;
use think\facade\Db;

class Member extends AdminBaseController
{
    private $memberTableName = 'wc_card_member';

    public function index()
    {
        return redirect("/{$this->module}/web.member.wxapp/main");
    }

    public function main()
    {
        $keyword    = $this->params['keyword'];
        $status     = $this->params['status'];
        $searchTime = trim($this->params["searchtime"]);

        $startTime = strtotime("-1 month");
        $endTime   = time();

        $condition = [
            'uniacid' => $this->uniacid,
        ];

        if (!empty($type)) {
            $status = $type;
        }

        if (is_numeric($status)) {
            $condition['status'] = $status;
        }

        if (!empty($searchTime) && is_array($this->params["time"]) && in_array($searchTime, array("create"))) {
            $startTime = strtotime($this->params["time"]["start"]);
            $endTime   = strtotime($this->params["time"]["end"]);

            $condition[$searchTime . "time"] = Db::raw("between {$startTime} and {$endTime} ");
        }

        if (!empty($keyword)) {
            $condition[''] = Db::raw(" nickname like '%" . trim($keyword) . "%' or realname like '%" . trim($keyword) . "%' or mobile like '%" . trim($keyword) . "%' ");
        }

        $list  = MemberServiceFacade::getList($condition, "*", "id desc", $this->pIndex, $this->pSize);
        $total = MemberServiceFacade::getTotal($condition);
        $pager = pagination2($total, $this->pIndex, $this->pSize);

        foreach ($list as &$item) {
            $item['avatar']    = tomedia($item['avatar']);
            $item['cardTotal'] = MemberCardServiceFacade::getTotal(['mid' => $item['id']]);
        }
        unset($item);

        $result = [
            'list'  => $list,
            'pager' => $pager,
            'total' => $total,

            'starttime' => $startTime,
            'endtime'   => $endTime,
        ];
        return $this->template('list', $result);
    }

    public function edit()
    {
        return $this->post();
    }

    public function add()
    {
        return $this->post();
    }

    public function post()
    {
        $id = $this->params['id'];

        if ($this->request->isPost()) {
            $data = array(
                'uniacid'  => $this->uniacid,
                'nickname' => trim($this->params['nickname']),
                'avatar'   => trim($this->params['avatar']),
                'realname' => trim($this->params['realname']),
                'mobile'   => trim($this->params['mobile']),
                'gender'   => trim($this->params['gender']),
            );
            if (!empty($this->params['birthday_str'])) {
                $birthdayDate       = strtotime($this->params['birthday_str']);
                $data['birthyear']  = date('Y', $birthdayDate);
                $data['birthmonth'] = date('m', $birthdayDate);
                $data['birthday']   = date('d', $birthdayDate);
            }
            if (!empty($id)) {
                Db::name($this->memberTableName)->where(['id' => $id])->update($data);
            } else {
                $id = Db::name($this->memberTableName)->insertGetId($data);
            }
            show_json(1, array('url' => webUrl('web.member/edit', array('id' => $id))));
        }

        $item = Db::name($this->memberTableName)->where(['id' => $id])->find();

        $item['birthday_str'] = $item['birthyear'] . '-' . $item['birthmonth'] . '-' . $item['birthday'];

        return $this->template('post', ['item' => $item]);
    }

    public function card()
    {
        $keyword    = $this->params['keyword'] ?? '';
        $status     = $this->params['status'] ?? '';
        $mid        = $this->params['mid'] ?? 0;
        $searchTime = trim($this->params["searchtime"] ?? '');

        $startTime = strtotime("-1 month");
        $endTime   = time();

        $condition = [
            'c.deleted' => 0,
        ];

        if (!empty($mid)) {
            $memberCardList = MemberCardServiceFacade::getAll(['mid' => $mid], "card_id");
            $condition[]    = Db::raw('id in (' . implode(",", array_column($memberCardList, "card_id")) . ')');
        }

        if (!empty($searchTime) && is_array($this->params["time"]) && in_array($searchTime, array("create"))) {
            $startTime = strtotime($this->params["time"]["start"]);
            $endTime   = strtotime($this->params["time"]["end"]);

            $condition["c." . $searchTime . "time"] = Db::raw("between {$startTime} and {$endTime} ");
        }

        if (!empty($keyword)) {
            $condition[''] = Db::raw(" c.card_no like '%" . trim($keyword) . "%' ");
        }

        $field = "c.*";
        $list  = Db::name("wc_card_card")->alias("c")->field($field)->where($condition)->select();
        $total = Db::name("wc_card_card")->alias("c")->where($condition)->count();
        $pager = pagination2($total, $this->pIndex, $this->pSize);

        foreach ($list as &$item) {
            $lastOrderRechargeInfo = OrderRechargeServiceFacade::getInfo(['orderid' => $item['last_orderid']], "id,recharge_id,recharge_name,recharge_desc,recharge_price,recharge_days");
            if (!empty($lastOrderRechargeInfo)) {
                $item['recharge_name']  = $lastOrderRechargeInfo['recharge_name'];
                $item['recharge_desc']  = $lastOrderRechargeInfo['recharge_desc'];
                $item['recharge_price'] = $lastOrderRechargeInfo['recharge_price'];
                $item['recharge_days']  = $lastOrderRechargeInfo['recharge_days'];
            }
        }
        unset($item);

        $result = [
            'list'  => $list,
            'pager' => $pager,
            'total' => $total,

            'starttime' => $startTime,
            'endtime'   => $endTime,
        ];
        // dump($result);die;
        return $this->template('card', $result);
    }

    public function cardDetail()
    {
        $cardId = $this->params['id'];

        $field = "c.*";
        $item  = Db::name("wc_card_card")->alias("c")->field($field)->where(['c.id' => $cardId])->find();

        $lastOrderRechargeInfo = OrderRechargeServiceFacade::getInfo(['orderid' => $item['last_orderid']], "id,recharge_id,recharge_name,recharge_desc,recharge_price,recharge_days");
        if (!empty($lastOrderRechargeInfo)) {
            $item['recharge_name']  = $lastOrderRechargeInfo['recharge_name'];
            $item['recharge_desc']  = $lastOrderRechargeInfo['recharge_desc'];
            $item['recharge_price'] = $lastOrderRechargeInfo['recharge_price'];
            $item['recharge_days']  = $lastOrderRechargeInfo['recharge_days'];
        }

        $rechargeList = OrderRechargeServiceFacade::getRechargeList($item['id']);

        $result = [
            'item'         => $item,
            'rechargeList' => $rechargeList,
        ];
        return $this->template('cardDetail', $result);
    }

}
